#!/bin/sh
if [ "$(uci -q get openmptcprouter.latest_versions)" = "" ]; then
	if [ "$(uci -q get unbound.@unbound[0].listen_port | grep 5353)" = "" ]; then
		uci -q batch <<-EOF >/dev/null
			set unbound.@unbound[-1].listen_port=5353
			set unbound.@unbound[-1].protocol="ip4_only"
			set unbound.@unbound[-1].enabled=1
			set unbound.@unbound[-1].recursion="aggressive"
			commit unbound
		EOF
	fi
	if [ "$(uci -q show dhcp | grep 127.0.0.1#5353)" = "" ]; then
		uci -q batch <<-EOF >/dev/null
			add_list dhcp.@dnsmasq[-1].server="127.0.0.1#5353"
			add_list dhcp.@dnsmasq[-1].server="/lan/"
			#add_list dhcp.@dnsmasq[-1].server="223.5.5.5#53"
			#add_list dhcp.@dnsmasq[-1].server="223.6.6.6#53"
			#add_list dhcp.@dnsmasq[-1].server="180.76.76.76#53"
			#add_list dhcp.@dnsmasq[-1].server="208.67.222.222#5353"
			#add_list dhcp.@dnsmasq[-1].server="208.67.220.220#5353"
			commit dhcp
		EOF
	fi
fi
if [ "$(uci -q show dhcp | grep 'use-application-dns.net')" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list dhcp.@dnsmasq[-1].server="/use-application-dns.net/"
		commit dhcp
	EOF
fi

if [ "$(uci -q get dhcp.@dnsmasq[0].rebind_domain)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set dhcp.@dnsmasq[-1].noresolv=1
		set dhcp.@dnsmasq[-1].nonegcache=1
		del dhcp.@dnsmasq[-1].interface
		add_list dhcp.@dnsmasq[-1].rebind_domain="plex.direct"
		commit dhcp
	EOF
fi
if [ "$(uci -q get dhcp.lan.dhcp_options)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		uci add_list dhcp.lan.dhcp_option='252,"\n"'
		commit dhcp
	EOF
fi

if [ "$(uci -q get https-dns-proxy.@https-dns-proxy[0].listen_port)" = "5053" ]; then
#	uci -q batch <<-EOF >/dev/null
#		delete https-dns-proxy.@https-dns-proxy[-1]
#		delete https-dns-proxy.@https-dns-proxy[-1]
#		commit https-dns-proxy
#	EOF
	/etc/init.d/https-dns-proxy stop
	/etc/init.d/https-dns-proxy disable
#		set https-dns-proxy.@https-dns-proxy[-1]=https-dns-proxy
#		set https-dns-proxy.@https-dns-proxy[-1].listen_addr='127.0.0.1'
#		set https-dns-proxy.@https-dns-proxy[-1].user='nobody'
#		set https-dns-proxy.@https-dns-proxy[-1].group='nogroup'
#		set https-dns-proxy.@https-dns-proxy[-1].bootstrap_dns='1.1.1.1,1.0.0.1'
#		set https-dns-proxy.@https-dns-proxy[-1].resolver_url='https://cloudflare-dns.com/dns-query'
#		set https-dns-proxy.@https-dns-proxy[-1].listen_port='5354'
fi

#uci -q batch <<-EOF >/dev/null
#	set unbound.@unbound[-1].add_local_fqdn=0
#	set unbound.@unbound[-1].localservice=1
#	set unbound.@unbound[-1].rebind_localhost=0
#	set unbound.@unbound[-1].rebind_protection=1
#	set unbound.@unbound[-1].unbound_control=1
#	commit unbound
#EOF


rm -f /tmp/luci-indexcache

exit 0
